var util = require('../../utils/util.js');

// pages/expertDetail/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    expertDetail: {},
    admin_id: 0,
    shareNum: '',
    recom_uid: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.userInfo = util.isLogin();
    let that = this;
    this.setData({
      admin_id: options.id
    })
    let userInfo = wx.getStorageSync('userInfo');
    console.log(this.data.recom_uid)
    if (options.recom_uid) {
      this.data.recom_uid = options.recom_uid
      
      wx.setStorage({
        key: 'recomUid',
        data: options.recom_uid
      })
    }
   
    util.http({
      url: '/api/miniwechat/line/guide',
      data: {
        uid: options.id
      }
    }).then(res => {
      console.log(res.recent_hot)
      if (res.recent_hot) {
        res.recent_hot.forEach((item, index) => {
          if (!item.line_title) item.line_title = ''
        })
      }
     

      that.setData({
        expertDetail: res
      })
    }).catch(err => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },

  back: function () {
    if (this.data.recom_uid != '') {
      wx.reLaunch({
        url: '/pages/index/index'
      })
    } else {
      wx.navigateBack({
        delta: 1
      })
    }
  },

  attention: function() {
    let that = this;
    let userInfo = wx.getStorageSync('userInfo');
    util.http({
      url: 'api/miniwechat/index/follow',
      method: 'POST',
      data: {
        admin_id: this.data.admin_id,
        user_id: userInfo.user_id,
        type: (that.data.expertDetail.is_follow == 0) ? 1 : 0
      }
    }).then(res => {
      that.data.expertDetail.is_follow = (this.data.expertDetail.is_follow == 0) ? 1 : 0
      that.setData({
        expertDetail: that.data.expertDetail
      })
    }).catch(err => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },
  makePhoneCall() {
    wx.makePhoneCall({
      phoneNumber: this.data.expertDetail.complaint_phone
    })
  },
  goRouteDetail(e) {
    wx.navigateTo({
      url: '/pages/routeDetail/index?id=' + e.currentTarget.dataset.id,
    })
  },
  goComplaint(e) {
    wx.navigateTo({
      url: '/pages/complaint/index?id=' + this.data.expertDetail.person_info.uid + '&name=' + this.data.expertDetail.person_info.nickname,
    })
  },
  shareShow(e) {
    this.setData({
      isShare: true
    })
  },
  shareHide(e) {
    this.setData({
      isShare: false
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: this.data.expertDetail.person_info.nickname,
      imageUrl: this.data.expertDetail.person_info.home_bg_img,
      path: 'pages/expertDetail/index?id=' + this.data.admin_id + '&recom_uid=' + this.data.userInfo.user_id
    }
  },
  posterShow(e) {
    this.setData({
      isPoster: true,
      isShare: false
    })
    this.getPosters();
  },
  posterHide(e) {
    this.setData({
      isPoster: false
    })
  },
  getPosters() {
    util.http({
      url: 'api/miniwechat/index/qrcode_user',
      method: 'POST',
      data: {
        user_id: this.data.userInfo.user_id,
        target_uid: this.data.admin_id
      }
    }).then((data) => {
      this.setData({
        posters: data
      })
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },
  saveImg(e) {
    let that = this;
    wx.authorize({
      scope: 'scope.writePhotosAlbum',
      success() {
        wx.downloadFile({
          url: that.data.posters,
          success(res) {
            if (res.statusCode === 200) {
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success(res) {
                  wx.showToast({
                    title: '保存成功',
                    icon: 'none',
                    duration: 2000
                  })
                  that.posterHide();
                },
                fail(err) {
                  wx.showToast({
                    title: '保存失败',
                    icon: 'none',
                    duration: 2000
                  })
                }
              })
            }
          }
        })
      },
      fail() {
        wx.showToast({
          title: '未获取授权',
          icon: 'none',
          duration: 2000
        })
      }
    })

  }
})